package com.wjzjq.hiring.company.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wjzjq.hiring.common.Constant;
import com.wjzjq.hiring.company.dao.ApplicationDao;

/**
 * Servlet implementation class ApplicationServlet
 */
@WebServlet("/ApplicationServlet")
public class ApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ApplicationDao applicationDao = new ApplicationDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String command = request.getParameter("command");
		if ("pass".equals(command)) {
			pass(request, response);
		} else if ("refuse".equals(command)) {
			refuse(request, response);
		} else if ("deleteApplication".equals(command)) {
			deleteApplication(request, response);

		}
	}

	private void deleteApplication(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int appId = Integer.valueOf(request.getParameter("applicationId"));
		int row = applicationDao.delete(appId);
		try {
			if (row == 0) {
				response.getWriter().print("fail");
			} else {
				response.getWriter().println("success");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void refuse(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int applicationId = Integer.valueOf(request.getParameter("applicationId"));
		int row = 0;
		row = ApplicationDao.changeStatus(Constant.STATUS_REFUSE, applicationId);
		if (row == 0) {
			try {
				response.getWriter().print("fail");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void pass(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int applicationId = Integer.valueOf(request.getParameter("applicationId"));
		int row = 0;
		row = ApplicationDao.changeStatus(Constant.STATUS_PASS, applicationId);
		if (row == 0) {
			try {
				response.getWriter().print("fail");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
